package com.zhuchaooasystem.presenter.home.fragment;

import android.app.Activity;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.OrderStateEntity;
import com.zhuchaooasystem.entity.ServiceEntity;
import com.zhuchaooasystem.entity.UserInfo;

import java.util.HashMap;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class ServicePresenter extends BasePresenter<ServiceContract.View> implements ServiceContract.Presenter {

    public ServicePresenter(Activity activity, ServiceContract.View view) {
        super(activity, view);
    }

//    @Override
//    public void downServiceData(final int page, String isOnWeek) {
//        Map<String, String> map = new HashMap<>();
//        map.put("memberId", UserInfo.getPersonalPk() + "");
//        map.put("PageIndex", page + "");
//        map.put("isOnWeek", isOnWeek);
//
//
//        Subscription subscription = HttpFactory.getHttpApi().getService(map)
//                .compose(SchedulersCompat.applyIoSchedulers())
//                .compose(RxResultHelper.handleResult())
//                .subscribe(new Action1<Object>() {
//                    @Override
//                    public void call(Object o) {
//
//                        ServiceEntity entity = (ServiceEntity) o;
//
//                        if (entity != null) {
//
//                            if (page == 1) {
//                                mView.showData(entity);
//                            } else if (page > 1) {
//                                mView.showMoreData(entity);
//                            }
//                        }
//
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//
//                        handleError(throwable);
//                    }
//                });
//        addSubscrebe(subscription);
//    }

    @Override
    public void downServiceStateData(final int page, String isOnWeek, String code) {
        Map<String, String> map = new HashMap<>();
        map.put("memberId", UserInfo.getPersonalPk() + "");
        map.put("PageIndex", page + "");
        map.put("isOnWeek", isOnWeek);
        map.put("status", code);
        Subscription subscription = HttpFactory.getHttpApi().getService(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {

                        ServiceEntity entity = (ServiceEntity) o;

                        if (entity != null) {
                            if (page == 1) {
                                mView.showData(entity);
                            } else if (page > 1) {
                                mView.showMoreData(entity);
                            }
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void downOrderState() {
        Subscription subscription = HttpFactory.getHttpApi().getOrderState()
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {

                        OrderStateEntity entity = (OrderStateEntity) o;

                        if (entity != null) {
                            mView.showOrderState(entity);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        handleError(throwable);

                    }
                });
        addSubscrebe(subscription);
    }
}
